/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package astroim.smack;

import astroim.samp.ProxyClient;
import java.util.ArrayList;
import java.util.Collection;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.DefaultListModel;
import org.astrogrid.samp.client.SampException;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.RosterListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.packet.Presence;

/**
 *
 * @author alykhalid
 */
public class Connection {

    private ConnectionConfiguration connConfig = null;
    private XMPPConnection connection = null;
    private astroim.smack.Credentials cred;
    public DefaultListModel ContactsOnline = new DefaultListModel();
    public ArrayList<ProxyClient> RegisteredProxyClients = new ArrayList<ProxyClient>();

    public XMPPConnection getConnection() {
        return connection;
    }

    private Connection() {
        cred = astroim.smack.Credentials.getInstance();
        connConfig = new ConnectionConfiguration(cred.getHost(), cred.getPort(), cred.getServiceName());
        //connConfig.setDebuggerEnabled(true);
        connection = new XMPPConnection(connConfig);
    }

    public Connection Connect() throws XMPPException {
       
        connection.connect();
        //sd.IntercepPacket(connection);
        //sd.register(connection).login(cred.getUserName(), cred.getPassword());
        connection.login(cred.getUserName(), cred.getPassword());
        SrvDisco sd = new SrvDisco();
        Presence presence = new Presence(Presence.Type.available);
        connection.sendPacket(presence);
        connection.getChatManager().addChatListener(CustomChatListener.getInstance());
        return this;
    }

    public void Disconnect() {
        connection.disconnect();
    }

    public Connection Roster() {
        connection.getRoster().addRosterListener(new RosterListener() {

            public void entriesAdded(Collection<String> clctn) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            public void entriesUpdated(Collection<String> clctn) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            public void entriesDeleted(Collection<String> clctn) {
                throw new UnsupportedOperationException("Not supported yet.");
            }

            public void presenceChanged(Presence prsnc) {
                String JID = prsnc.getFrom();
                if (prsnc.isAvailable()) {
                    if (!ContactsOnline.contains(JID)) {
                        ContactsOnline.addElement(JID);
                    }
                } else {
                    if (ContactsOnline.contains(JID)) {
                        ContactsOnline.removeElement(JID);
                        ArrayList<Integer> RegisteredProxyClientsIndex = new ArrayList<Integer>();
                        int index = 0;
                        for(ProxyClient pc : RegisteredProxyClients){
                            index++;
                            if(pc.ClientName.equalsIgnoreCase(JID))
                                try {
                                pc.unregisterFromHub();
                                RegisteredProxyClientsIndex.add(index);
                            } catch (SampException ex) {
                                Logger.getLogger(Connection.class.getName()).log(Level.SEVERE, null, ex);
                            }
                        }
                        for(Integer ind : RegisteredProxyClientsIndex){
                            RegisteredProxyClients.remove(ind);
                        }
                    }
                }
            }
        });

        return this;
    }

    public static Connection getInstance() {
        return ConnectionHolder.INSTANCE;
    }

    private static class ConnectionHolder {

        private static final Connection INSTANCE = new Connection();
    }
}
